﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using System.Transactions;
using Components;
using ServiceHelper.Doc;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Buy3a00Manager : Buy3a00Base
    {
        private Buy3a00DAL buy3a00Dal = new Buy3a00DAL();

        private static Buy3a00Manager _entity;

        public static Buy3a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Buy3a00Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Buy3a00 model = null;
            Buy3a01 dtlModel = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            if ("W" == action) //收货
            {
                //表头
                var helper = new AcceptManager();
                Sto8a00 sto8A00 = null;

                try
                {
                    sto8A00 = helper.ParseDoc(tb);
                    helper.Parse(sto8A00);

                    foreach (DataRow row in dtl.Rows)
                        helper.ParseRow(row);

                    var errorInfo = helper.Save();

                    if (!string.IsNullOrEmpty(errorInfo))
                        return errorInfo;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

                return Json.ToString(new
                {
                    status = true,
                    id = sto8A00.Listid
                });
            }


            //使用该类的原因是为了生成连续的单号
            using (TransactionScope scope = new TransactionScope())
            {
                if ("A" == action) //新增
                {
                    //保存概要表身
                    model = new Buy3a00();
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    //生成采购单号
                    model.Docno = buy3a00Dal.GetDocno(Components.Validation.ToString(tb["plistid"]), usercode);
                    model.Listid = Sys0a60Manager.Entity.CreateSn("buy3a00_listid", usercode);
                    model.Usercode = appstr;
                    model.Audstatus = 1;
                    lst.Add(this.Add(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Buy3a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);
                        dtlModel.Listid =
                            Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("buy3a01_listid", usercode));
                        dtlModel.Plistid = model.Listid;

                        lst.Add(Buy3a01Manager.Entity.Add(dtlModel));
                    }

                    LogHelper.Entity.Out(string.Format("新增->采购单号：{0}，共保存{1}明细记录", model.Docno, lst.Count - 1));
                }
                else if ("M" == action) //修改
                {
                    model = this.GetModelById(Components.Validation.ToString(tb["listid"]));
                    if (null == model) return "该笔订单可能已经被删除！";
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    lst.Add(this.Update(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Buy3a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);

                        if (!Components.Validation.IsNull(dr["listid"]))
                        {
                            lst.Add(Buy3a01Manager.Entity.Modify(dtlModel));
                        }
                        else
                        {
                            dtlModel.Listid =
                                Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("buy3a01_listid",
                                                                                           usercode));
                            dtlModel.Plistid = model.Listid;

                            lst.Add(Buy3a01Manager.Entity.Add(dtlModel));
                        }
                    }

                    LogHelper.Entity.Out(string.Format("修改->采购单号：{0}，共保存{1}明细记录", model.Docno, lst.Count - 1));

                    if (null != deldata)
                    {
                        foreach (DataRow dr in dtl.Rows)
                        {
                            dtlModel = new Buy3a01();
                            dtlModel.Listid = Components.Validation.ToInt(dr["listid"]);
                            lst.Add(Buy3a01Manager.Entity.Delete(dtlModel));
                        }
                    }
                }
                else if ("P" == action) //审核
                {
                    lst.Add(buy3a00Dal.Audit(Components.Validation.ToString(tb["listid"])));
                }
                else if ("D" == action)
                {
                    lst.Add(buy3a00Dal.DeleteDocno(Components.Validation.ToString(tb["listid"])));
                }
                else if("C" == action)
                {
                    lst.Add(buy3a00Dal.UpdateAcceptComplete(Components.Validation.ToString(tb["listid"])));
                }

                string msg = CommonManager.Entity.SaveData(lst);
                if (!string.IsNullOrEmpty(msg)) //是否提交事务
                    scope.Dispose();
                else
                    scope.Complete();

                return msg;
            }
        }

        #endregion

        public SqlApp DoAdjust(string rowId, double qty, double amt)
        {
            return buy3a00Dal.DoAdjust(rowId, qty, amt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SqlApp Arrival(string flag, Buy3a01 model, string arrno, DateTime arrdate)
        {
            return buy3a00Dal.Arrival(flag, model, arrno, arrdate);
        }
    }
}



